package com.member.web.controller.shop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.BusinessType;
import com.member.common.utils.poi.ExcelUtil;
import com.member.shop.domain.SysBizCouponReceive;
import com.member.shop.domain.SysBizOrderGoods;
import com.member.shop.domain.SysBizRechargeUser;
import com.member.shop.domain.SysBizShopCertification;
import com.member.shop.service.ISysBizRechargeUserService;
import com.member.shop.service.ISysBizShopCertificationService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 【用户充值】控制器
 *
 * @author lixinchen
 * @version 1.0
 * @date 2024-05-08
 **/
@RestController
@RequestMapping("/shop/rechargeUser")
public class SysBizRechargeUserController extends BaseController {
    @Resource
    private ISysBizRechargeUserService sysBizRechargeUserService;

    @Resource
    private ISysBizShopCertificationService sysBizShopCertificationService;

    private void setTenantId(SysBizRechargeUser sysBizRechargeUser) {
        Long tenantId = getTenantId();
        if (tenantId != null && tenantId > 0) {
            sysBizRechargeUser.setTenantId(tenantId);
        }
    }

    /**
     * 查询用户充值列表
     */
    @PreAuthorize("@ss.hasPermi('shop:rechargeUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizRechargeUser sysBizRechargeUser) {
        startPage();
        setTenantId(sysBizRechargeUser);
        List<SysBizRechargeUser> list = sysBizRechargeUserService.selectSysBizRechargeUserList(sysBizRechargeUser);

        if(!CollectionUtils.isEmpty(list)){
            for (SysBizRechargeUser rechargeUser : list) {
                List<SysBizShopCertification> sysBizShopCertification = sysBizShopCertificationService.selectSysBizShopCertificationList(
                        new LambdaQueryWrapper<SysBizShopCertification>().eq(SysBizShopCertification::getUserId,rechargeUser.getUserId())
                                .eq(SysBizShopCertification::getDeleted,0)
                );
                if (!CollectionUtils.isEmpty(sysBizShopCertification)) {
                    rechargeUser.setNickName(sysBizShopCertification.get(0).getShopName());
                }
            }

        }
        return getDataTable(list);
    }

    /**
     * 导出用户充值列表
     */
    @PreAuthorize("@ss.hasPermi('shop:rechargeUser:export')")
    @Log(title = "用户充值", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizRechargeUser sysBizRechargeUser) {
        setTenantId(sysBizRechargeUser);
        List<SysBizRechargeUser> list = sysBizRechargeUserService.selectSysBizRechargeUserList(sysBizRechargeUser);
        ExcelUtil<SysBizRechargeUser> util = new ExcelUtil<SysBizRechargeUser>(SysBizRechargeUser.class);
        util.exportExcel(response, list, "用户充值数据");
    }

    /**
     * 获取用户充值详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:rechargeUser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysBizRechargeUserService.selectSysBizRechargeUserById(id));
    }

    /**
     * 新增用户充值
     */
    @PreAuthorize("@ss.hasPermi('shop:rechargeUser:add')")
    @Log(title = "用户充值", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizRechargeUser sysBizRechargeUser) {
        return toAjax(sysBizRechargeUserService.insertSysBizRechargeUser(sysBizRechargeUser));
    }

    /**
     * 修改用户充值
     */
    @PreAuthorize("@ss.hasPermi('shop:rechargeUser:edit')")
    @Log(title = "用户充值", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizRechargeUser sysBizRechargeUser) {
        return toAjax(sysBizRechargeUserService.updateSysBizRechargeUser(sysBizRechargeUser));
    }

    /**
     * 删除用户充值
     */
    @PreAuthorize("@ss.hasPermi('shop:rechargeUser:remove')")
    @Log(title = "用户充值", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysBizRechargeUserService.deleteSysBizRechargeUserByIds(ids));
    }
}
